package pe.hongi.frist;

import pe.hongi.frist.credu_replay.AndroidBridge;
import pe.hongi.frist.credu_replay.MYWebViewClient;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.webkit.JsPromptResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class ImageWebView extends Activity
{   
	
	WebView webview;
	
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.image);
		
		webview = (WebView)findViewById(R.id.webview);
		webview.getSettings().setJavaScriptEnabled(true);
		webview.getSettings().setPluginsEnabled(true);
        webview.setWebViewClient(new MYWebViewClient());
		showweb();
		
		Bundle extras = getIntent().getExtras();         
	    if(extras!=null){
	    	if(extras.getString("image_url")!=null){
	    		         
	    	String off_url=extras.getString("image_url");
	    	
	        webview.loadUrl(off_url);
	    	
	    	}
	    }
	    
       
		
	}
	
public class MYWebViewClient extends WebViewClient{
		
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			// TODO Auto-generated method stub
			
			webview.loadUrl(url);
			return super.shouldOverrideUrlLoading(view, url);
		}
		@Override
		public void onPageFinished(WebView view, String url) {
			// TODO Auto-generated method stub
			
			
		super.onPageFinished(view, url);
			
		}
		
		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
		
			
			super.onPageStarted(view, url, favicon);
		}
	}

	public void showweb(){
	   
	  
		webview.getSettings().setPluginsEnabled(true);
		webview.getSettings().setJavaScriptEnabled(true);  
		
		webview.addJavascriptInterface(new AndroidBridge(), "android");
		
		final Context myApp = this;
		webview.setWebChromeClient(new WebChromeClient() {
		    @Override
		    public boolean onJsAlert(WebView view, String url, String message, final android.webkit.JsResult result)
		    {
		        new AlertDialog.Builder(myApp)
		            .setTitle("AlertDialog")
		            .setMessage(message)
		            .setPositiveButton(android.R.string.ok,
		                    new AlertDialog.OnClickListener()
		                    {
		                        public void onClick(DialogInterface dialog, int which)
		                        {
		                            result.confirm();
		                        }
		                    })
		            .setCancelable(false)
		            .create()
		            .show();
		        result.confirm();
		        return true;
		    };
		    
		    @Override
		    public boolean onJsPrompt(WebView view, String url, String message,
		    		String defaultValue, JsPromptResult result) {
		    	// TODO Auto-generated method stub
		    
		    
	    	return super.onJsPrompt(view, url, message, defaultValue, result);
	    	
		    }
		    
	   });
    }
	 public class AndroidBridge{
		   
		   public void callAndroid(final String arg){
			   
			Handler handler=new Handler();
			handler.post(new Runnable(){

				public void run() {
					// TODO Auto-generated method stub
					   Log.v("Tag","callAndroid("+arg+")");
					   webview.loadUrl("http://m.imba.ac.kr/imba/servlet/controller.mobile.main.MainServlet?p_process=mainPage");
				   }
			   });
			   
		   }
		   
	   }
}
